package com.lib.online.model;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * @author ievgen_s
 */

@XmlRootElement
@XmlType(propOrder = { "genre_id", 
		"name",
	"description"
	})
@Entity
@Table(name="genres")
@Cacheable
public class Genre implements Serializable, Comparable<Genre>{
   
   
    private static final long serialVersionUID = -6171048431441820633L;
    private Integer genre_id;
    private String name;
     private String description;
     @XmlTransient
     @JsonIgnore
     private List<Reading> books;
    
    public Genre() {}
     
    public Genre(Integer genre_id, String name, String description) {
	
    	this.genre_id=genre_id;
    	this.name = name;
    	this.description = description;	
    	this.books = new ArrayList<Reading>();
    }

    @Id 
	@Column(name="genre_id")
    @GeneratedValue  
    public Integer getGenre_id() {
		return genre_id;
	}

	public void setGenre_id(Integer genre_id) {
		this.genre_id = genre_id;
	}

	@Column(name="name")
	public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    @Column(name="description")
    public String getDescription() {
        return description;
    }


    public void setDescription(String description) {
        this.description = description;
    }

    @ManyToMany (targetEntity=com.lib.online.model.Reading.class,
    		cascade = {CascadeType.PERSIST, CascadeType.MERGE},
    		fetch=FetchType.EAGER)
    
    @JoinTable(name="genres_books",
    joinColumns = @JoinColumn(name="genre_id_fk"),
    inverseJoinColumns = @JoinColumn(name="book_id_fk")
    )
    @XmlTransient
    @JsonIgnore   
    public List<Reading> getBooks() {
		return books;
	}

	public void setBooks(List<Reading> books) {
		this.books = books;
	}

	@Override
    public String toString() {
	return name;
    }

    @Override
    public int compareTo(Genre g) {
	return this.name.compareTo(g.getName());	
    }

}